package com.jingyanzi.sys.impl;

import com.jingyanzi.sys.entity.SysFileManagementExample;
import com.jingyanzi.sys.dao.SysFileManagementMapper;
import com.jingyanzi.sys.domain.FileManagementDO;
import com.jingyanzi.sys.domain.result.ResultDO;
import com.jingyanzi.sys.domain.result.ResultSupport;
import com.jingyanzi.sys.domain.service.IFileManagementService;
import com.jingyanzi.sys.entity.SysFileManagement;
import com.jingyanzi.sys.util.BeanUtilsExtends;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/*@Service("fileManagementService")
public class FileManagementServiceImpl extends BaseServiceImpl implements IFileManagementService {
    @Autowired
    private SysFileManagementMapper fileManagementMapper;

    private String fileMangePath;

    private String urlPrefix;

    public FileManagementServiceImpl() {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("application.properties");
        Properties p = new Properties();
        try {
            p.load(inputStream);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        fileMangePath = p.getProperty("filemanagement.path");
        urlPrefix = p.getProperty("urlprefix");
    }

    public FileManagementServiceImpl(String fileMangePath){
        this.fileMangePath = fileMangePath;
    }

    @Override
    public ResultDO add(FileManagementDO fileMangementDO) {

        int insert;
        ResultDO result=new ResultSupport(true);
        if(fileMangementDO==null) {
            result.setSuccess(false);
            return result;
        }
        try {
            SysFileManagement fileManagement = new SysFileManagement();

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
            Calendar calendar = Calendar.getInstance();
            String dateName = dateFormat.format(calendar.getTime());


            String origFilePath = fileMangementDO.getOrigFile();

            FileSystemResource inputFileResource = new FileSystemResource(origFilePath);

            if (!inputFileResource.exists()) {
                result.setErrorCode("输入的文件不存在");
                return result;
            }

            String saveName = leadNo() + origFilePath.substring(origFilePath.lastIndexOf('.'));
            String savePath = fileMangePath + "/" + fileMangementDO.getCategory() + "/" + dateName;

            new File(savePath).mkdirs();

            FileSystemResource outputFileResource = new FileSystemResource(savePath + "/" + saveName);

            FileCopyUtils.copy(inputFileResource.getInputStream(), outputFileResource.getOutputStream());

            BeanUtilsExtends.copyProperties(fileManagement, fileMangementDO);

            fileManagement.setUrlPrefix(urlPrefix);
            fileManagement.setRefCount(1);
            fileManagement.setIsPublic(true);
            fileManagement.setSaveName(saveName);
            fileManagement.setSavePath(savePath);
            if (fileManagement.getGenTime() == null)
                fileManagement.setGenTime(calendar.getTime());

            insert = fileManagementMapper.insert(fileManagement);
            if (insert > 0) {
                fileMangementDO.setId(fileManagement.getId());
                fileMangementDO.setUrl(fileManagement.getUrlPrefix() + '/' + fileManagement.getCategory() +
                        '/' +  dateName + '/' + fileManagement.getSaveName());
                result.setModel(ResultDO.FIRST_MODEL_KEY, fileMangementDO);
            } else {
               throw new Exception("添加失败，条数为零");
            }

        }catch (Exception e){
            result.setSuccess(false);
            result.setErrorMsg("添加失败");
        }

        return result;
    }

    @Override
    public ResultDO get(Integer id) {

        ResultDO result = new ResultSupport(true);

        FileManagementDO fileManagementDO = new FileManagementDO();
        SysFileManagementExample  example = new SysFileManagementExample();
        try{
            example.createCriteria().andIdEqualTo(id);
            List<SysFileManagement> list  = fileManagementMapper.selectByExample(example);
            SysFileManagement sysFileManagement = list.get(0);
            BeanUtilsExtends.copyProperties(fileManagementDO,sysFileManagement);

            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
            fileManagementDO.setUrl(sysFileManagement.getUrlPrefix() + '/' + sysFileManagement.getCategory() +
                    '/' + formatter.format(sysFileManagement.getGenTime()) + '/' + sysFileManagement.getSaveName());
        }catch (Exception e){
            result.setSuccess(false);
            result.setErrorMsg("后台异常" + e.toString());
        }

        result.setModel(ResultDO.FIRST_MODEL_KEY, fileManagementDO);
        result.setSuccess(true);
        return result;
    }

    public String leadNo() {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddssHHmmss");
            Calendar calendar = Calendar.getInstance();
            String dateName = dateFormat.format(calendar.getTime());
            int i = (int) (Math.random() * (999999 - 100000)) + 100000;
            dateName = dateName + "" + i;
            return dateName;
    }


}
*/